/* 
	@名称: index
	@功能: 主页js
*/

$(function(){
	//标签切换函数	
	function tabChange(tab,selector,target1,target2){
		$(tab).hover(function(){
			$(this).addClass(selector).siblings().removeClass(selector);
			var index = $(this).index();
			$(target1).eq(index).removeClass("fn-hide").siblings(target1).addClass("fn-hide");
			$(target2).eq(index).removeClass("fn-hide").siblings(target2).addClass("fn-hide");
		});
	}
	//热门车型
	tabChange(".cartype-tab li","selected",".cartype-desc ul",".index-hottype table")
	
	//左侧视频、活动、新闻列表
	tabChange(".index-left-infolist .index-tab-title li","current",".index-left-infolist .content")

	//右侧就业培养、汽车美图
	tabChange(".index-right-list .index-tab-title li","current",".index-right-list .content")
	
	//名企
	tabChange(".company li","current",".company-con")
	
	//人才招聘
	tabChange(".job li","current",".index-job-con")	
	
	
	//图片轮转
	function picRotate(obj){
		var index=0;//图片索引
		var rotateTimer;//时间戳
		var $picList = obj.find("ul:first");//图片列表
		var len = $picList.children("li").length//列表中图片个数
		var picWidth = $picList.find("li:first").width();//图片宽度
		var $vector = obj.find(".index-vector span");//圆点导航
		obj.hover(function(){
			clearInterval(rotateTimer);
		},function(){
			rotateTimer = setInterval(function(){
				index++;
				$vector.eq(index).addClass("active").siblings().removeClass("active");
				if(index==len){
					index=0;
					$vector.eq(index).addClass("active").siblings().removeClass("active");
					$picList.animate({"marginLeft":"0px"},500);
				}else{
					$picList.animate({"marginLeft":-(index*picWidth)+"px"},500);
				}
			},3000);
		}).trigger("mouseout");
		
		
		var $pre = obj.find(".pre");//左移按钮
		var $next = obj.find(".next");//右移按钮
		//左移
		$next.click(function(){
			index++;
			$vector.eq(index).addClass("active").siblings().removeClass("active");
			if(index==len){
				index=0;
				$vector.eq(index).addClass("active").siblings().removeClass("active");
				$picList.animate({"marginLeft":"0px"},500);
			}else{
				$picList.animate({"marginLeft":-(index*picWidth)+"px"},500);
			}
		});
		//右移
		$pre.click(function(){
			index--;
			$vector.eq(index).addClass("active").siblings().removeClass("active");
			if(index<0){
				index=len-1;
				$picList.animate({"marginLeft":-(index*picWidth)+"px"},500);
			}else{
				$picList.animate({"marginLeft":-(index*picWidth)+"px"},500);
			}
		});
		
		//圆点导航
		$vector.click(function(){
			$(this).addClass("active").siblings().removeClass("active");
			index = $(this).index();
			$picList.animate({"marginLeft":-(index*picWidth)+"px"},500);
		});
	}
	var $topRotate = $("#js-top-rotate");//头部图片轮转
	picRotate($topRotate);
	var $indusRotate = $("#js-indus-rotate");//行业资讯图片轮转
	picScroll($indusRotate,0);
	var $commuRotate = $("#js-commu-rotate");//社区图片轮转
	picRotate($commuRotate);
	
	
	//图片移动
	function picMove(obj,margin){
		var $picList = obj.find("ul:first");
		var picWidth = $picList.find("li:first").width();
		$picList.animate({"marginLeft":-(picWidth+margin)+"px"},500,function(){
			$picList.css({marginLeft:0}).find("li:first").appendTo($picList);
		});
	}
	//滚动图片
	function picScroll(object,margin){
		var scrollTimer;
		object.hover(function(){
			clearInterval(scrollTimer);
		},function(){
			scrollTimer = setInterval(function(){
				picMove(object,margin);
			},3000)
		}).trigger("mouseout");
		
		//左移
		var $pre = object.find(".pre");
		$pre.click(function(){
			picMove(object,margin);
		});
		//右移
		var $next = object.find(".next");
		$next.click(function(){
			var $picList = object.find("ul:first");
			var picWidth = $picList.find("li:first").width();
			$picList.css({marginLeft:-(picWidth+margin)+"px"}).find("li:last").prependTo($picList);
			$picList.animate({"marginLeft":"0px"},500);
		});
	}
	var $traffic = $("#traffic-img-scroll");
	var $industry = $("#indus-img-scroll");
	var $sell = $("#sell-img-scroll");
	var $buy = $("#buy-img-scroll");
	picScroll($traffic,18);//交通
	picScroll($industry,18);//行业资讯
	picScroll($sell,18);//招商
	picScroll($buy,18);//供求
	
	
	//咨询对话框
	$(".consult-dialog-close").hover(function(){
		$(this).addClass("active")
	},function(){
		$(this).removeClass("active")
	});
	//关闭
	$(".consult-dialog-close").click(function(){
		$("#consult-dialog").addClass("fn-hide");
	});
	$(".index-consult-list ul li a").click(function(){
		var title = $(this).attr("title");
		var aid = $(this).attr("aid");
		showAnswer(	33713, title);
	});
	//46653
	function showAnswer (aid, title) {
	    if (!aid) {
	        alert('信息传递错误, 请重新操作!');
	    } else {
		
	        $.post("http://www.ixdcw.com/live/ajax.php",{ 'action':'answer', 'aid':aid },function(data){
	            if (data) {
	                $('.index-consult-con p').text(data);
					$("#consult-dialog").removeClass("fn-hide");
					$('.index-consult-con h3').text(title);
	            }else{alert("false");}
	        });
	    }
	}
});